/**
 * @author goufahui 1344116821@qq.com
 * @version 0.0.3
 * @description 公共方法
 */

/**
 * @author goufahui 1344116821@qq.com
 * @description 对象数组去重
 * @param { arrayObject:对象数组,attribute:去重属性}
 * @returns {[]}
 */
export function arrayObjectDeduplication(arrayObject, attribute) {
  let map = new Map()
  for (let item of arrayObject) {
    if (!map.has(item[attribute])) {
      map.set(item[attribute], item)
    }
  }
  return [...map.values()]
}

/**
 * @author goufahui 1344116821@qq.com
 * @description 对象深拷贝
 * @param { dest:目标对象,obj:实列对象} //let newObj=objectDeep({},obj)
 * @returns {}
 */
export function objectDeep(dest, obj) {
  let o = dest
  for (let key in obj) {
    if (typeof obj[key] === 'object') {
      //判断是不是对象
      o[key] = obj[key].constructor === Array ? [] : {}
      deep(o[key], obj[key])
    } else {
      o[key] = obj[key]
    }
  }
  return o
}

/**
 * @author goufahui 1344116821@qq.com
 * @description 随机字符串
 * @param { len:长度}
 * @returns {}
 */
export function randomString(len) {
  length = len || 40
  const characters =
      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  let str = ''
  for (let i = 0; i < length; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length)
    str += characters[randomIndex]
  }
  return str
}

/**
 * @author goufahui 1344116821@qq.com
 * @description 判断按钮权限
 * @param { key:按钮唯一标识}
 * @returns Boolean
 */
export function checkPermission(key) {
  const matched = this.$route.matched
  let path = matched[0].path + '/' + this.$route.name
  console.log(sessionStorage.menus)
  lo
  let menu = JSON.parse(sessionStorage.menus)
  // 把树结构数据打平为一维数组
  const getData = (arr) => {
    let result = []
    arr.forEach((item) => {
      let res = JSON.parse(JSON.stringify(item))
      delete res.child
      result.push(res)
      if (item.child && item.child.length > 0) {
        result = result.concat(getData(item.child))
      }
    })
    return result
  }
  let arr = []
  getData(menu).forEach((element) => {
    if (element.url == path) {
      element.buttonArray.forEach((item) => {
        arr.push(item.no)
      })
    }
  })
  return arr.some((item) => item == key)
}
